package constant.milk.toilet.setting;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import constant.milk.toilet.MainActivity;
import constant.milk.toilet.R;
import constant.milk.toilet.SuperActivity;
import constant.milk.toilet.broadcast.PushBroadcast;
import constant.milk.toilet.dialog.MilkDialog;
import constant.milk.toilet.dialog.MilkPushDialog;
import constant.milk.toilet.dialog.MilkPushDialog.PushCollBank;
import constant.milk.toilet.timer.TimerManager;
import constant.milk.toilet.util.MilkUtil;

public class SettingActivity extends SuperActivity
{
	private static final int REQUEST_CODE_PASSWORD = 0;
	private static final int REQUEST_CODE_PUSH = 1;
	private static final int REQUEST_CODE_FONT = 2;
	
	private TextView preView;
	private TextView titleTextView;
	
	private View passwordView;
	private TextView passwordOnOffTextView;
	private TextView passwordTipTextView;
	
	private View pushView;
	private TextView pushOnOffTextView;
	private TextView pushTipTextView;
	
	private View timerAlarmView;
	private TextView timerAlarmOnOffTextView;
	private TextView timerAlarmTipTextView;
	
	private View fontView;
	private TextView fontTypeTextView;
	private TextView fontTipTextView;
	
	private View initView;
	private TextView initTipTextView;
	
	private View bankUpView;
	private TextView bankUpTipTextView;
	
	private View emailView;
	private TextView emailTipTextView;
	
	private View versionView;
	private TextView versionTipTextView;
	
	private MilkDialog dialog;
	private MilkPushDialog timerDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.setting_actvity);
		
		setFindViewId();
		setOnClick();
		setData();
	}
	
	@Override
	protected void setFindViewId()
	{
		preView = (TextView) findViewById(R.id.settingPreView);
		
		titleTextView = (TextView) findViewById(R.id.settingTitleTextView);
		
		passwordView = findViewById(R.id.settingPasswordView);
		passwordOnOffTextView = (TextView) findViewById(R.id.settingPasswordOnOffTextView);
		passwordTipTextView = (TextView) findViewById(R.id.settingPasswordTipTextView);
		
		pushView = findViewById(R.id.settingPushView);
		pushOnOffTextView = (TextView) findViewById(R.id.settingPushOnOffTextView);
		pushTipTextView = (TextView) findViewById(R.id.settingPushTipTextView);
		
		timerAlarmView = findViewById(R.id.settingTimerAlarmView);
		timerAlarmOnOffTextView = (TextView) findViewById(R.id.settingTimerAlarmOnOffTextView);
		timerAlarmTipTextView = (TextView) findViewById(R.id.settingTimerAlarmTipTextView);
		
		fontView = findViewById(R.id.settingFontView);
		fontTypeTextView = (TextView) findViewById(R.id.settingFontTypeTextView);
		fontTipTextView = (TextView) findViewById(R.id.settingFontTipTextView);
		
		initView = findViewById(R.id.settingInitView);
		initTipTextView = (TextView) findViewById(R.id.settingInitTipTextView);
		
		bankUpView = findViewById(R.id.settingBackUpView);
		bankUpTipTextView = (TextView) findViewById(R.id.settingBackUpTipTextView);
		
		emailView = findViewById(R.id.settingEmailView);
		emailTipTextView = (TextView) findViewById(R.id.settingEmailTipTextView);
		
		versionView = findViewById(R.id.settingVersionView);
		versionTipTextView = (TextView) findViewById(R.id.settingVersionTipTextView);
	}

	@Override
	protected void setOnClick()
	{
		preView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				finish();
			}
		});
		
		passwordView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (passwordOnOffTextView.getText().toString().equals(getString(R.string.on)))
				{
					passwordOnOffTextView.setText(getString(R.string.off));
					
					SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
					SharedPreferences.Editor e = p.edit();
					
					e.putInt(getString(R.string.prefPassword), 0);
					e.commit();
				}
				else
				{
					Intent intent = new Intent(superContext, SettingPasswordActivity.class);
					startActivityForResult(intent, REQUEST_CODE_PASSWORD);
				}
			}
		});
		
		pushView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (pushOnOffTextView.getText().toString().equals(getString(R.string.on)))
				{
					SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
					SharedPreferences.Editor e = p.edit();
					
					e.putInt(getString(R.string.prefPushAlarm), 0);
					e.commit();
					
					Intent intent = new Intent(superContext, PushBroadcast.class);    
					PendingIntent sender = PendingIntent.getBroadcast(superContext, 1, intent, 
							PendingIntent.FLAG_CANCEL_CURRENT);
					
			        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
			        manager.cancel(sender);
			        sender.cancel();
					
					pushOnOffTextView.setText(getString(R.string.off));
				}
				else
				{
					SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
					SharedPreferences.Editor e = p.edit();
					
					e.putInt(getString(R.string.prefPushAlarm), 1);
					e.commit();
					
					Intent intent = new Intent(superContext, PushBroadcast.class);    
					PendingIntent sender = PendingIntent.getBroadcast(superContext, 1, intent, 
							PendingIntent.FLAG_CANCEL_CURRENT);
					
			        AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
			        manager.cancel(sender);
			        
			        int version = Build.VERSION.SDK_INT;
					
					if (version >= 19)
					{
						manager.setExact(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (24 * 60 * 60000), sender);
					}
					else
					{
						manager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (24 * 60 * 60000), sender);
					}
					
					pushOnOffTextView.setText(getString(R.string.on));
				}
			}
		});
		
		timerAlarmView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (timerAlarmOnOffTextView.getText().toString().equals(getString(R.string.off)))
				{
					timerDialog = new MilkPushDialog(superContext, new PushCollBank()
					{
						@Override
						public void getPush(int pushTime)
						{
							SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
							SharedPreferences.Editor e = p.edit();
							
							e.putInt(getString(R.string.prefTimerAlarm), pushTime);
							e.commit();
							
							timerAlarmOnOffTextView.setText(pushTime + " 분");
							
							if (p.getBoolean(getString(R.string.prefTimerOnOff), false))
							{
								TimerManager timerManager = new TimerManager();
								timerManager.startAlarm(superContext);
							}
						}
					});
					
					timerDialog.show();
				}
				else
				{
					SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
					SharedPreferences.Editor e = p.edit();
					
					e.putInt(getString(R.string.prefTimerAlarm), 0);
					e.commit();
					
					timerAlarmOnOffTextView.setText(getString(R.string.off));
					
					TimerManager timerManager = new TimerManager();
					timerManager.stopAlarm(superContext);
				}
			}
		});
		
		fontView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, FontActivity.class);
				startActivityForResult(intent, REQUEST_CODE_FONT);
			}
		});
		
		initView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				dialog = new MilkDialog(superContext, 
						getString(R.string.app_name) + "를 초기화 하시겠습니까?", 
						new OnClickListener()
						{
							@Override
							public void onClick(View v)
							{
								dialog.dismiss();
								
								try
								{
									SharedPreferences pref = getSharedPreferences("pref", Activity.MODE_PRIVATE);
									SharedPreferences.Editor editor = pref.edit();
									
									editor.remove(getString(R.string.prefPassword));
									editor.remove(getString(R.string.prefPasswordData));
									editor.remove(getString(R.string.prefFont));
									
									editor.commit();
									
									superDb.createTable(superContext);
									
									superDb.deleteTable(superContext);
								} catch (Exception e)
								{
									MilkUtil.showToast(superContext, "초기화에 실패 하였습니다. 개발자에게 문의 하여 주세요.");
								}
								
								Intent intent = new Intent(superContext, MainActivity.class);
								intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
								startActivity(intent);
								finish();
							}
						}, 
						new OnClickListener()
						{
							@Override
							public void onClick(View v)
							{
								dialog.dismiss();
							}
						});
				
				dialog.show();
			}
		});
		
		bankUpView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, BackUpActvitiy.class);
				startActivity(intent);
			}
		});
		
		emailView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				String [] email = new String[]{getString(R.string.email)};
				
				Intent intent = new Intent(Intent.ACTION_SEND);
				intent.putExtra(Intent.EXTRA_EMAIL, email);
				intent.setType("plain/text");
				intent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name) + " 문의사항");
				startActivity(intent);
			}
		});
		
		versionView.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
			}
		});
	}

	@Override
	protected void setData()
	{
		titleTextView.setTextColor(Color.WHITE);
		
		titleTextView.setTextSize(TEXT_SIZE + 2);
		passwordTipTextView.setTextSize(TEXT_SIZE - 3);
		pushTipTextView.setTextSize(TEXT_SIZE - 3);
		timerAlarmTipTextView.setTextSize(TEXT_SIZE - 3);
		fontTipTextView.setTextSize(TEXT_SIZE - 3);
		initTipTextView.setTextSize(TEXT_SIZE - 3);
		bankUpTipTextView.setTextSize(TEXT_SIZE - 3);
		emailTipTextView.setTextSize(TEXT_SIZE - 3);
		versionTipTextView.setTextSize(TEXT_SIZE - 3);
		
		SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
		int passwordFlag = p.getInt(getString(R.string.prefPassword), 0);
		int fontFlag = p.getInt(getString(R.string.prefFont), 1);
		int timerAlarm = p.getInt(getString(R.string.prefTimerAlarm), 5);
		int pushAlarm = p.getInt(getString(R.string.prefPushAlarm), 1);
		
		if (passwordFlag > 0)
		{
			passwordOnOffTextView.setText(getString(R.string.on));
		}
		else
		{
			passwordOnOffTextView.setText(getString(R.string.off));
		}
		
		if (timerAlarm == 0)
		{
			timerAlarmOnOffTextView.setText(getString(R.string.off));
		}
		else
		{
			timerAlarmOnOffTextView.setText(timerAlarm + " 분");
		}
		
		if (pushAlarm > 0)
		{
			pushOnOffTextView.setText(getString(R.string.on));
		}
		else
		{
			pushOnOffTextView.setText(getString(R.string.off));
		}
		
		setFont(fontFlag);
		
		PackageInfo pi = null;
		String version = "";
		
		try
		{
			pi = getPackageManager().getPackageInfo(getPackageName(), 0);
			version = pi.versionName;
		} catch (Exception e)
		{
			e.printStackTrace();
			version = "";
		}
		
		versionTipTextView.setText(version);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == REQUEST_CODE_PASSWORD)
		{
			String str = data.getStringExtra(SettingPasswordActivity.PASSWORD_DATA);
			
			SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
			SharedPreferences.Editor e = p.edit();
			
			e.putInt(getString(R.string.prefPassword), 1);
			e.putString(getString(R.string.prefPasswordData), str);
			e.commit();
			
			passwordOnOffTextView.setText(getString(R.string.on));
		}
		else if (requestCode == REQUEST_CODE_PUSH)
		{
			
		}
		else if (requestCode == REQUEST_CODE_FONT)
		{
			int str = data.getIntExtra(FontActivity.FONT_DATA, 1);
			
			SharedPreferences p = getSharedPreferences("pref", Activity.MODE_PRIVATE);
			SharedPreferences.Editor e = p.edit();
			
			e.putInt(getString(R.string.prefFont), str);
			e.commit();
			
			Intent intent = new Intent(superContext, MainActivity.class);
			intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
			startActivity(intent);
			finish();
		}
	}
	
	private void setFont(int flag)
	{
		if (flag == 0)
		{
			fontTypeTextView.setText(getString(R.string.small));
		}
		else if (flag == 1)
		{
			fontTypeTextView.setText(getString(R.string.normal));
		}
		else
		{
			fontTypeTextView.setText(getString(R.string.big));
		}
	}

}
